// hyb_yl/mycenter/pages/twitterApply/twitterApply.js
var app = getApp()
var citys = require('../../../../utils/citys.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 1,
    cityData: [],
    region: [],
    isindex: 1,
    ischeck: true,
    issuss: false,
    money: 0.00,
    twoapplymoney: 0.00,
    bigMask: false,
    neworder:[{}],
    xieyi:false,
    neworder:[{}],
  },
  xieyibtn(){
    this.setData({
      xieyi: true
    })
  },
  closezhe: function () {
    this.setData({
      xieyi: false
    })
  },
  bindRegionChange: function (e) {
    console.log(e);
    this.setData({
      cityData: e.detail.value,
      isindex: 0
    })
  },
  gotkhome: function () {
    var that = this
    if (app.globalData.mytkid == '') {
      var id = that.data.id
      app.globalData.mytkid = id
    } else {
      var id = app.globalData.mytkid
    }
    wx.navigateTo({
      url: '/hyb_yl/mycenter/pages/Twitter/Twitter?id=' + id,
    })
  },
  gotkagin: function () {
    this.setData({
      step: 1
    })
  },
  formSubmit: function (e) {
    console.log(e);
    const that = this;
    const username = e.detail.value.username;
    const tel = e.detail.value.tel;
    const city = that.data.cityData;
    const check = that.data.ischeck;
    var tkid = app.globalData.tkid //一级
    var erjituike = app.globalData.mytkid //二级
    var money = e.detail.value.money;
    var tkusername = that.data.tkusername
    var twoapplymoney = that.data.twoapplymoney
    console.log(tkid,erjituike)
    //查询二级开关是否开启
    if (that.data.erjiopen == 0 && that.data.tkid !== 0) {
      wx.showToast({
        title: '无法注册二级分销商，请联系管理员',
        icon: 'none',
      })
      return false
    }

    if (username == '') {
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none'
      })
      return;
    } else if (tel == '') {
      wx.showToast({
        title: '请输入您的手机号',
        icon: 'none'
      })
      return;
    } else if (!(/^1[34578]\d{9}$/.test(tel))) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return;
      if (phone.length >= 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'none'
        })
        return;
      }
    } else if (city.length == 0) {
      wx.showToast({
        title: '请选择所在地',
        icon: 'none'
      })
      return;
    } else if (!check) {
      wx.showToast({
        title: '请同意协议',
        icon: 'none'
      })
      return;
    }
    if (that.data.erjiopen == 1 && that.data.tkid !== 0) {
      //查询是否审核
      app.util.request({
        'url': 'entry/wxapp/index.addtuikeerji',
        data: {
          username: username,
          tel: tel,
          city: city,
          openid: wx.getStorageSync('openid'),
          tkid: tkid,
          money: money
        },
        success: function (res) {
          app.util.request({
            'url': 'entry/wxapp/index.updatetuike',
            data: {
              tkid: tkid,
              mytkid: erjituike,
              type: 1,
              openid: wx.getStorageSync('openid'),
              content: '通过' + tkusername + '分享进入',
              zid: 0
            },
            success: function (res) {
              //查询我的id
              app.util.request({
                'url': 'entry/wxapp/index.usertuike',
                data: {
                  openid: wx.getStorageSync('openid')
                },
                success: function (res) {
                  var id = res.data.id
                  wx.showToast({
                    title: '提交成功',
                  })
                  setTimeout(function () {
                    console.log(that.data.twoexamine, 'wwwwwwwwwwwwwwwwwwwwwwwwwwww')
                    if (that.data.twoexamine == 1) {
                      that.setData({
                        step: 2
                      })
                    } else {
                      wx.navigateTo({
                        url: '/hyb_yl/mycenter/pages/Twitter/Twitter?id=' + id,
                      })
                    }

                  }, 800)
                }
              })

            }
          })

        }
      })
      return false
    }
    if (that.data.erjiopen == 2 && that.data.tkid !== 0) {
      app.util.request({
        'url': 'entry/wxapp/tuike.pay',
        method: "GET",
        data: {
          openid: wx.getStorageSync('openid'),
          z_tw_money: twoapplymoney
        },
        success: function (res) {
          app.util.request({
            'url': 'entry/wxapp/index.addtuikeerji',
            data: {
              username: username,
              tel: tel,
              city: city,
              openid: wx.getStorageSync('openid'),
              tkid: tkid,
              money: money
            },
            success: function (res) {
              app.util.request({
                'url': 'entry/wxapp/index.updatetuike',
                data: {
                  tkid: tkid,
                  mytkid: erjituike,
                  type: 1,
                  openid: wx.getStorageSync('openid'),
                  content: '通过' + tkusername + '分享进入',
                  zid: 0
                },
                success: function (res) {
                  console.log(res)
                }
              })
              wx.showToast({
                title: '提交成功',
              })
              setTimeout(function () {
                that.setData({
                  step: 2
                })
              }, 800)
            }
          })
        }
      })
    } else {
      if (money > 0.00) { //支付
        app.util.request({
          'url': 'entry/wxapp/tuike.pay',
          method: "GET",
          data: {
            openid: wx.getStorageSync('openid'),
            z_tw_money: money
          },
          success: function (res) {
            console.log(res)
            wx.requestPayment({
              'timeStamp': res.data.timeStamp,
              'nonceStr': res.data.nonceStr, //字符串随机数
              'package': res.data.package,
              'signType': res.data.signType,
              'paySign': res.data.paySign,
              success: function (res) {
                console.log(res)

                app.util.request({
                  'url': 'entry/wxapp/index.addtuike',
                  data: {
                    username: username,
                    tel: tel,
                    city: city,
                    openid: wx.getStorageSync('openid'),
                    tkid: tkid,
                    money: money
                  },
                  success: function (res) {
                    console.log(res)
                    if (that.data.iftk == 0) {
                      //新用户给上级返佣金
                      app.util.request({
                        'url': 'entry/wxapp/tuike.register',
                        data: {
                          tkid: tkid,
                          mytkid: erjituike,
                          openid: wx.getStorageSync('openid')
                        },
                        success: function (res) {
                          console.log(res)
                        }
                      })
                      app.util.request({
                        'url': 'entry/wxapp/index.updatetuike',
                        data: {
                          tkid: tkid,
                          mytkid: erjituike,
                          type: 1,
                          openid: wx.getStorageSync('openid'),
                          content: '通过' + tkusername + '分享进入',
                          zid: 0
                        },
                        success: function (res) {
                          console.log(res)
                        }
                      })
                    }
                    wx.showToast({
                      title: '提交成功',
                    })
                    setTimeout(function () {
                      that.setData({
                        step: 2
                      })
                    }, 800)
                  }
                })
              },
              fail: function (error) {
                // console.log(error);
              },
            })
          }
        })
      } else {
        app.util.request({
          'url': 'entry/wxapp/index.addtuike',
          data: {
            username: username,
            tel: tel,
            city: city,
            openid: wx.getStorageSync('openid'),
            tkid: tkid,
            money: money
          },
          success: function (res) {
            console.log(res)

            if (that.data.iftk == 0) {
              //新用户给上级返佣金
              app.util.request({
                'url': 'entry/wxapp/tuike.uptopmoney',
                data: {
                  tkid: tkid,
                  mytkid: erjituike,
                  openid: wx.getStorageSync('openid')
                },
                success: function (res) {
                  console.log(res)
                }
              })
              app.util.request({
                'url': 'entry/wxapp/index.updatetuike',
                data: {
                  tkid: tkid,
                  mytkid: erjituike,
                  type: 1,
                  openid: wx.getStorageSync('openid'),
                  content: '通过' + tkusername + '分享进入',
                  zid: 0
                },
                success: function (res) {
                  console.log(res)
                }
              })
            }
            wx.showToast({
              title: '提交成功',
            })
            setTimeout(function () {
              that.setData({
                step: 2
              })
            }, 800)
          }
        })
      }
    }



  },
  checkboxChange: function (e) {
    console.log(e);
    const value = e.detail.value.length;
    if (value == 0) {
      this.setData({
        ischeck: false
      })
    } else {
      this.setData({
        ischeck: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/tuike.roul',
      success: function (res) {
        console.log(res)
        that.setData({
          money: res.data.applymoney,
          twoapplymoney: res.data.twoapplymoney,
          twoexamine: res.data.twoexamine,
          content:res.data.content
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/index.neworder',
      data: {
        type: 'tuike'
      },
      success: function (res) {
        that.setData({
          neworder: res.data
        })
      },
      fail: function (err) {
        
      },
    })
    if (options.scene) {
      var info = decodeURIComponent(options.scene)
      //上级代理id
      var tkid = info.split("id=")[1];
      console.log(tkid)
    } else {
      var tkid = app.globalData.tkid //一级id
    }
    that.setData({
      tkid: tkid
    })
    //
    //查询上级
    app.util.request({
      'url': 'entry/wxapp/index.shangjiuserselect',
      data: {
        tkid: tkid,
      },
      success: function (res) {
        console.log(res)
        that.setData({
          erjituike: res.data.tkid,
          tkusername: res.data.username
        })
      }
    })
    //是否开启二级分销
    app.util.request({
      'url': 'entry/wxapp/index.erjiopen',
      data: {
        tkid: tkid,
      },
      success: function (res) {
        console.log(res)
        that.setData({
          erjiopen: res.data
        })
      }
    })
    //查询我是否是推客
    app.util.request({
      'url': 'entry/wxapp/index.usertuike',
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (res) {
        console.log(res)
        if (res.data !== 0) {
          //是推客
          that.setData({
            iftk: 1
          })
          if (res.data.tkid == 0) {
            //查询推客入驻金额
            console.log('222')
            app.util.request({
              'url': 'entry/wxapp/tuike.roul',
              success: function (res) {
                console.log(res)
                that.setData({
                  money: res.data.applymoney,
                  twoapplymoney: res.data.twoapplymoney,
                  twoexamine: res.data.twoexamine,
                })
              }
            })
          } else {
            //查询推客入驻金额
            app.util.request({
              'url': 'entry/wxapp/tuike.roul',
              data: {
                tkid: that.data.tkid
              },
              success: function (res) {
                console.log(res)
                that.setData({
                  omoney: res.data.applymoney,
                  twoapplymoney: res.data.twoapplymoney,
                  twoexamine: res.data.twoexamine,
                })
              }
            })
          }

        } else {
          //不是推客
          that.setData({
            iftk: 0
          })
          app.util.request({
            'url': 'entry/wxapp/tuike.roul',
            success: function (res) {
              console.log(res)
              that.setData({
                money: res.data.applymoney,
              })
            }
          })
        }
        if (res.data !== 0) {
          that.setData({
            id: res.data.id
          })
          if (res.data.state == '0') {
            that.setData({
              step: 2
            })
          } else if (res.data.state == '1') {
            that.setData({
              step: 3,
              issuss: true
            })
          }
        } else {
          that.setData({
            step: 1
          })
        }

      }
    })
    this.gettkrulo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var openid = wx.getStorageSync('openid')

    console.log(openid)
    if (openid) {
      this.setData({
        bigMask: true
      })
    } else {
      this.setData({
        bigMask: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  gettkrulo() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/index.tuikrulo',
      success: function (res) {
        console.log(res)
        that.setData({
          info: res.data
        })
      }
    })
  }
})